$.fn.extend({
    fadeIn: function () {
        $(this).show().addClass("fade-in");
        setTimeout(() => {
            $(this).removeClass("fade-in");
        }, 800);
        return this;
    },
    fadeInSlow: function () {
        $(this).show().addClass("fade-in-slow");
        setTimeout(() => {
            $(this).removeClass("fade-in-slow");
        }, 1200);
        return this;
    },
    fadeUp: function () {
        $(this).show().addClass("fade-up");
        setTimeout(() => {
            $(this).removeClass("fade-up");
        }, 800);
        return this;
    },
    shake: function () {
        $(this).show().addClass("wobble");
        setTimeout(() => {
            $(this).removeClass("wobble");
        }, 800);
        return this;
    }
});
const index = {
    init: function () {
        this.countHeight();
        this.allEvent();
        this.getData();
        this.role = {};
        this.data = {};
        this.step = 0;
        this.dialogList = [];//对话框
        this.currentDialog = {};
        this.dialogIndex = 0;
        this.currentBg = "";
        this.currentFg = "";
        this.timeIndex = 200;//图文出现延迟
        this.clickIndex = 600;//点击限制
        this.dayEndCount = {
            one: 0,
            two: 0,
            three: 0
        };
        this.currentMusic = "";
        this.currentVideo = "";
        this.audioFlag = true;//第一个音频播放器
        this.startId = 0;
        this.currentBgMusic = "";
        this.audioBgFlag = true;
        $("#bgAudio1")[0].volume = 0.3;//设置背景音乐音量
        $("#bgAudio2")[0].volume = 0.3;
        this.getUser();
        this.bgPlay = true;
        this.saveGameList = [];
        this.imgObj = {};
        this.muCount = 2;
        this.muPreLoadDone = false;
    },
    allEvent: function () {
        let _this = this;
        let video = document.getElementById("video");
        let timeControl = true;
        $(".play-start").on("click", function () {
            $(".play-tips").fadeIn().siblings(".hideItem").hide();
        });
        $("play-start-wrap,.play-tips,.play-os-wrap,.play-dialog-wrap,.play-phone-wrap,#fg").on("click", function () {//顺序节点
            if (timeControl) {
                timeControl = false;
                _this.loadEle();
                setTimeout(() => {
                    timeControl = true;
                }, _this.clickIndex);
            }
        });

        $(".play-mu").on("click", function () {

            if (timeControl) {
                timeControl = false;
                if (_this.muPreLoadDone) {
                    _this.muPreLoadDone = false;
                    _this.loadEle();
                    _this.progress();

                }
                setTimeout(() => {
                    timeControl = true;
                }, _this.clickIndex);
            }
        });
        $(".info-reply-body").on("click", ".reply-item", function (e) {//分支节点
            if (timeControl) {
                timeControl = false;
                //
                $("#audioClick")[0].play();
                let index = $(this).data("index");
                let data = {}, nodeData = {};
                for (let i = 0; i < _this.step.length; i++) {//选择下次节点
                    let currentData = _this.data[_this.step[i]];
                    if (index == currentData.id) {
                        _this.step = currentData.nextStep;
                        _this.proloadData();
                        break;
                    }
                }
                let countData = _this.data[index];
                _this.playAudio(countData);
                _this.addBgMusic(countData);
                //计算结局
                let countIndex = $(this).index();
                if (countData.isTriggerEnd == "1") {
                    //console.log("强强侠结局")
                    if (_this.role[3].score >= 110) {
                        _this.step = 318;
                        _this.saveGame(318);
                    }
                } else if (countData.isTriggerEnd == "2") {//日常节点结算
                    //console.log("juanjuan")
                    if (countIndex == 0) {
                        _this.dayEndCount.one++;
                        _this.dayEndCount.two = 0;
                        _this.dayEndCount.three = 0;
                    } else if (countIndex == 1) {
                        _this.dayEndCount.two++;
                        _this.dayEndCount.one = 0;
                        _this.dayEndCount.three
                    } else if (countIndex == 2) {
                        _this.dayEndCount.three++;
                        _this.dayEndCount.one = 0;
                        _this.dayEndCount.two = 0;
                    }
                    if (_this.dayEndCount.one == 3) {
                        //console.log("长歌结局")
                        _this.step = 342;
                        _this.saveGame(342);
                    }
                    if (_this.dayEndCount.two == 6) {
                        //console.log("卷卷结局")
                        _this.step = 250;
                        _this.saveGame(250);
                    }
                    localStorage.setItem("dayEndCount", JSON.stringify(_this.dayEndCount));
                }
                let scoreObj = countData.extend;
                if (scoreObj) {//计算好感度
                    scoreObj = JSON.parse(scoreObj);
                    let addScore = scoreObj.addScore;
                    if (addScore) {
                        let roleId = addScore[0].roleId;
                        let score = addScore[0].score;
                        if (roleId && score) {
                            $("#score").text(_this.role[roleId].score);
                            _this.role[roleId].score += parseInt(score);
                            setTimeout(function () {
                                if (countData.id != "219") {
                                    if (score >= 0) {
                                        $("#audioLikeUp")[0].play();
                                    } else {
                                        $("#audioLikeDown")[0].play();
                                    }
                                    $(".score-tip").text(score >= 0 ? "+" + score : score).fadeUp();
                                    setTimeout(function () {
                                        $("#score").text(_this.role[roleId].score);
                                    }, 500);
                                    $(".info-like-item .img").css("background", "#fff url(" + _this.role[roleId].img + "?x-oss-process=image/resize,w_64,limit_0) no-repeat 0 0/34pt 34pt");
                                    $(".info-like").show();
                                    $(".info-like-item .icon").removeClass("rubber-band");
                                    setTimeout(() => {//好感度动画
                                        $(".info-like-item .icon").addClass("rubber-band");
                                    }, 100);
                                    setTimeout(function () {
                                        _this.loadEle();
                                        $(".info-like").hide();
                                    }, 1500);
                                } else {
                                    _this.loadEle();
                                }
                            }, 500);

                            setTimeout(() => {
                                timeControl = true;
                            }, _this.clickIndex);
                            //保存好感度到本地
                            let role = JSON.stringify(_this.role);
                            localStorage.setItem("role", role);
                            return;
                        }
                    }
                }

                _this.loadEle();
                //
                setTimeout(() => {
                    timeControl = true;
                }, _this.clickIndex);
            }
        });
        //视频事件
        $(video).on("click", function () {
            // if(this.paused){
            //     this.play();
            // }else{
            //     this.pause();
            // }
        });
        $(video).on("canplaythrough", function () {
            // console.log("canplay")
        });
        $(video).on("ended", function () {
            _this.loadEle();
        });
        $("#reload").on("click", function () {//重新来过
            _this.getData();
        });
        $(".play-icon").on("click", function () {
            if (!_this.bgPlay) {
                $(this).find(".play").show();
                $(this).find(".pause").hide();
                if (_this.audioBgFlag) {
                    $("#bgAudio1")[0].play();
                } else {
                    $("#bgAudio2")[0].play();
                }
            } else {
                $(this).find(".play").hide();
                $(this).find(".pause").show();
                $("#bgAudio1")[0].pause();
                $("#bgAudio2")[0].pause();
            }
            _this.bgPlay = !_this.bgPlay;
        });
        $("#more").on("click", function () {
            for (let item of $("ul.other-items li")) {
                let index = $(item).data("index");
                if (_this.saveGameList.indexOf(parseInt(index)) != -1) {
                    $(item).addClass("done");
                }
            }
            $(".end-other").fadeIn().siblings(".hideItem").hide();
        });
        $("ul.other-items li").on("click", function () {
//            let type=$(this).data("count");
//            let index=$(this).data("index");
//            // _this.jsApiCall(index,type);
//            _this.step=index;
//            _this.loadEle();
        });
        $("#share").on("click", function () {
            $(".end-share").show();
        });
        $(".end-share").on("click", function () {
            $(".end-share").hide();
        });

    },
    saveGame: function (id) {
        if (this.saveGameList.indexOf(id) == -1) {
            this.saveGameList.push(id);
        }
        localStorage.setItem("saveGame", JSON.stringify(this.saveGameList));
        if (id == "362") {
            $("#endText").text("失败结局");
            $(".end-head-img").css("background", "url(http://rpgstaticoss.gjgj.com/App/images/img01.png) no-repeat 0 0/100%");
        } else if (id == "351") {
            $("#endText").text("普通结局");
            $(".end-head-img").css("background", "url(http://rpgstaticoss.gjgj.com/App/images/img02.png) no-repeat 0 0/100%");
        } else if (id == "347") {
            $("#endText").text("完美结局");
            $(".end-head-img").css("background", "url(http://rpgstaticoss.gjgj.com/App/images/img03.png) no-repeat 0 0/100%");
        } else if (id == "318") {
            $("#endText").text("强强侠结局");
            $(".end-head-img").css("background", "url(http://rpgstaticoss.gjgj.com/App/images/img04.png) no-repeat 0 0/100%");
        } else if (id == "250") {
            $("#endText").text("卷卷结局");
            $(".end-head-img").css("background", "url(http://rpgstaticoss.gjgj.com/App/images/img05.png) no-repeat 0 0/100%");
        } else if (id == "342") {
            $("#endText").text("长歌结局");
            $(".end-head-img").css("background", "url(http://rpgstaticoss.gjgj.com/App/images/img06.png) no-repeat 0 0/100%");
        } else if (id == "248") {
            $("#endText").text("拐子结局");
            $(".end-head-img").css("background", "url(http://rpgstaticoss.gjgj.com/App/images/img07.png) no-repeat 0 0/100%");
        }
    },
    jsApiCall: function (index, type) {//支付
        var _this = this;
        var json = {
            "cash": 1,
            "type": type,
        };
        var successFunction = function (data) {
            if (data.status == "1") {
                wx.chooseWXPay({
                    timestamp: data.data.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                    nonceStr: data.data.nonceStr, // 支付签名随机串，不长于 32 位
                    package: data.data.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
                    signType: data.data.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
                    paySign: data.data.paySign, // 支付签名
                    success: function (res) {
                        // 支付成功后的回调函数
                        _this.step = index;
                        _this.loadEle();

                    }
                });
            } else if (data.status == -1) {
                alert(data.info);
            }
        };
        var errorFunction = function () {

        };
        $.ajax({
            url: "http://rpg.gjgj.com/Scenario/createOrder",
            data: json,
            type: "post",
            error: errorFunction,
            success: successFunction
        });
    },
    countHeight: function () {
        let windowHeight = document.body.clientHeight;
        let windowWidth = document.body.clientWidth;
        let height = 667 / 375 * windowWidth;
        let h = windowHeight > height ? height : windowHeight;
        $(".play-body").css("height", h + "px");
        $("#video").css({"width": windowWidth + "px", "height": h + "px"});
    },
    end: function () {
        share();//重设分享内容
        this.stopAudio();
        $(".play-end").show().siblings(".hideItem").hide();
        //
        this.removeScheudule();
        for (let item of $("ul.other-items li")) {
            let index = $(item).data("index");
            if (this.saveGameList.indexOf(parseInt(index)) != -1) {
                $(item).removeClass("gray");
            }
        }
    },
    loadEle: function () {
        //布局当前数据

        if (this.step instanceof Array) {
            if (!this.step.length) {
                this.end();
                return;
            }
            let str = "";
            for (let i = 0; i < this.step.length; i++) {
                let currentData = this.data[this.step[i]];
                str += '<div class="reply-item" data-index="' + currentData.id + '">' + currentData.character + '</div>';
            }
            if (this.data[this.step[0]].isTriggerEnd == 2) {
                $("#replyText").text("日常节点");
            } else {
                $("#replyText").text("请选择回复");
            }
            $(".info-reply-body").html(str);
            $(".play-info").show().siblings(".hideItem").hide().end().find(".info-reply").show();
            this.prepareNext();
        } else {
            if (!this.step) {
                this.end();
                return;
            }
            if (this.step == "248") {
                this.saveGame(248);//保存拐子成就
            }
            let currentData = this.data[this.step];
            this.addBgMusic(currentData);//添加背景乐
            this.playAudio(currentData);//添加旁边
            if (currentData.type == 1) {//视频
                if (currentData.videoUrl) {
                    if (currentData.videoUrl != this.currentVideo) {
                        $("#video").attr("src", currentData.videoUrl);
                    }
                    $("#video")[0].play();
                    setTimeout(function () {
                        $(".play-video").show().siblings(".hideItem").hide().end().find(".video-wrap").show();
                    }, 500);
                }
                setTimeout(() => {
                    this.prepareNext();
                }, 100);
            }
            if (currentData.type == 2) {// 图+文+音+角色
                if (currentData.nodeType == "5") {
                    $(".play-mu").fadeInSlow().siblings(".hideItem").hide();
                    $(".play-mu-name").html(currentData.character);
                    this.prepareNext();

                } else {
                    this.addBg(currentData);
                    this.showDialog(currentData, "showType");
                    this.prepareNext();
                }

            }
            if (currentData.type == 3) {//对话
                this.addBg(currentData);
                if (this.dialogIndex == 0) {
                    this.dialogList = currentData.extend.dialogue;
                    this.currentDialog = this.dialogList[0];
                    this.showDialog(this.currentDialog, "shouType");
                } else {
                    if (this.dialogIndex < this.dialogList.length) {
                        this.currentDialog = this.dialogList[this.dialogIndex];
                        this.showDialog(this.currentDialog, "shouType");
                        if (this.dialogIndex == this.dialogList.length - 1) {
                            this.prepareNext();
                            this.dialogIndex = 0;//重置对话
                            this.dialogList = [];
                            return;
                        }
                    }
                }
                this.dialogIndex++;
            }
            if (currentData.type == 4) {//电话
                this.addBg(currentData);
                this.addFg(currentData);
                $(".play-phone-wrap").fadeIn().siblings(".hideItem").hide();
                $(".play-phone").hide();// 对话开始先隐藏对话框
                if (currentData.character) {
                    $(".phone-name").text(currentData.roleName);
                    $(".phone-content").text(currentData.character);
                    $(".play-phone").fadeIn();
                }
                this.prepareNext();
            }

        }
    },
    stopAudio: function () {
        $("#audio1")[0].pause();//暂停音频
        $("#audio2")[0].pause();
        $("#bgAudio1")[0].pause();
        $("#bgAudio2")[0].pause();
    },
    addBgMusic: function (currentData) {
        let $audio = this.audioBgFlag ? $("#bgAudio1") : $("#bgAudio2");
        let $audioPause = this.audioBgFlag ? $("#bgAudio2") : $("#bgAudio1");
        if (currentData.backgroundMusicUrl) {
            if (currentData.backgroundMusicUrl != this.currentBgMusic) {
                $audio.attr("src", currentData.backgroundMusicUrl);
            }
            setTimeout(() => {
                $audioPause[0].pause();
                if (this.bgPlay) {
                    $audio[0].play();
                }
            }, 100);
        } else {
            $("#bgAudio1")[0].pause();
            $("#bgAudio2")[0].pause();
        }
    },
    playAudio: function (currentData) {
        let $audio = this.audioFlag ? $("#audio1") : $("#audio2");
        let $audioPause = this.audioFlag ? $("#audio2") : $("#audio1");
        if (currentData.musicUrl) {
            if (currentData.musicUrl != this.currentMusic) {
                $audio.attr("src", currentData.musicUrl);
            }
            setTimeout(function () {
                $audioPause[0].pause();
                $audio[0].play();
            }, 100);
        } else {
            $("#audio1")[0].pause();
            $("#audio2")[0].pause();
        }
    },
    showDialog: function (currentDialog, showType) {
        this.setScheudule(this.step);
        if (currentDialog[showType] == 1) {
            this.addFg(currentDialog);
            if (currentDialog.roleName) {//对话
                $("#bg").css("filter", "blur(3px)");
                $(".play-dialog-wrap").fadeIn().siblings(".hideItem").hide();
                $(".play-dialog").hide();// 对话开始先隐藏对话框
                if (currentDialog.character) {
                    this.addDialog(currentDialog);
                }
            } else {//心理活动
                $("#bg").css("filter", "initial");
                $(".play-os-wrap").siblings(".hideItem").hide();
                if (currentDialog.character) {
                    $(".os-content").text(currentDialog.character);
                    $(".play-os-wrap").fadeIn();
                } else {
                    $(".play-os-wrap").hide();
                }
            }
        } else if (currentDialog[showType] == 2) {
            this.addFg(currentDialog);
            setTimeout(() => {
                if (currentDialog.roleName) {//对话
                    $("#bg").css("filter", "blur(3px)");
                    $(".play-dialog-wrap").fadeIn().siblings(".hideItem").hide();
                    $(".play-dialog").hide();// 对话开始先隐藏对话框
                    if (currentDialog.character) {
                        this.addDialog(currentDialog);
                    }
                } else {//心理活动
                    $("#bg").css("filter", "initial");
                    $(".play-os-wrap").siblings(".hideItem").hide();
                    if (currentDialog.character) {
                        $(".os-content").text(currentDialog.character);
                        $(".play-os-wrap").fadeInSlow();
                    } else {
                        $(".play-os-wrap").hide();
                    }
                }
            }, this.timeIndex);
        } else if (currentDialog[showType] == 3) {
            if (currentDialog.roleName) {//对话
                $("#bg").css("filter", "blur(3px)");
                $(".play-dialog-wrap").fadeIn().siblings(".hideItem").hide();
                $(".play-dialog").hide();// 对话开始先隐藏对话框
                if (currentDialog.character) {
                    this.addDialog(currentDialog);
                }
            } else {//心理活动
                $("#bg").css("filter", "initial");
                $(".play-os-wrap").siblings(".hideItem").hide();
                if (currentDialog.character) {
                    $(".os-content").text(currentDialog.character);
                    $(".play-os-wrap").fadeInSlow();
                } else {
                    $(".play-os-wrap").hide();
                }
            }
            setTimeout(() => {
                this.addFg(currentDialog);
            }, this.timeIndex);
        }
    },
    addDialog: function (currentDialog) {
        if (currentDialog.roleId == "2") {
            $(".dialog-name").addClass("user");
            $(".dialog-name i").show().css("background", "url(" + currentDialog.roleImg + "?x-oss-process=image/resize,w_40,limit_0) center center/100% no-repeat");
        } else {
            $(".dialog-name").removeClass("user");
            $(".dialog-name i").hide();
        }
        $(".dialog-name span").text(currentDialog.roleName);
        $(".dialog-content").text(currentDialog.character);
        $(".play-dialog").fadeInSlow();
    },
    addBg: function (currentData) {
        if (currentData.backgroundImgUrl && this.currentBg != currentData.backgroundImgUrl) {
            if (currentData.backgroundImgUrl.indexOf(".gif") != -1) {
                $("#bg").css("background", "url(" + currentData.backgroundImgUrl + ") center top/100% no-repeat").fadeIn();
            } else {
                $("#bg").css("background", "url(" + currentData.backgroundImgUrl + "?x-oss-process=image/resize,w_375,limit_0) center center/100% no-repeat").fadeIn();
            }
            this.currentBg = currentData.backgroundImgUrl;
        }
        if (currentData.effects == "1") {
            $("#bg").shake();
        }
    },
    addFg: function (currentData) {
        if (currentData.foregroundImgUrl) {
            if (this.currentFg != currentData.foregroundImgUrl) {
                $("#fg").css("background", "url(" + currentData.foregroundImgUrl + "?x-oss-process=image/resize,w_375,limit_0) center center/100% no-repeat").fadeIn();
                this.currentFg = currentData.foregroundImgUrl;
            }
        } else {
            $("#fg").css("background", "initial");
            this.currentFg = "";
        }
    },
    prepareNext: function () {
        if (!(this.step instanceof Array)) {
            this.setScheudule(this.step);
            let currentData = this.data[this.step];
            if (currentData.nodeType == 3) {//计算结局 甄一一
                let user = this.role[4];
                // console.log("甄一一结局")
                if (user.score <= 60) {
                    this.step = 347;
                    this.saveGame(347);
                } else if (user.score > 60 && user.score < 80) {
                    this.step = 351;
                    this.saveGame(351);
                } else {
                    this.step = 362;
                    this.saveGame(362);
                }
                this.proloadData();
                return;
            }
            this.step = currentData.nextStep;

        }
        this.proloadData();
    },
    setScheudule: function (step) {
        let stepObj = {
            step: step,
            dialogList: this.dialogList,
            dialogIndex: this.dialogIndex
        }
        localStorage.setItem("stepObj", JSON.stringify(stepObj));
    },
    removeScheudule: function () {
        localStorage.removeItem("stepObj");
        localStorage.removeItem("role");
        localStorage.removeItem("dayEndCount");
    },
    proloadData: function () {
        if (this.step instanceof Array) {
            for (let i = 0; i < this.step.length; i++) {
                let data = this.data[this.step[i]];
                if (data.backgroundImgUrl) {
                    let image = new Image();
                    image.src = data.backgroundImgUrl + "?x-oss-process=image/resize,w_375,limit_0";
                }
                if (data.foregroundImgUrl) {
                    let image = new Image();
                    image.src = data.foregroundImgUrl + "?x-oss-process=image/resize,w_375,limit_0";
                }
            }
        } else {
            let data = this.data[this.step];
            if (data.backgroundImgUrl) {
                let image = new Image();
                image.src = data.backgroundImgUrl + "?x-oss-process=image/resize,w_375,limit_0";
            }
            if (data.foregroundImgUrl) {
                let image = new Image();
                image.src = data.foregroundImgUrl + "?x-oss-process=image/resize,w_375,limit_0";
            }
            if (data.musicUrl) {//预加载音频
                if (data.musicUrl != this.currentMusic) {

                    let $audio = this.audioFlag ? $("#audio2") : $("#audio1");
                    $audio.attr("src", data.musicUrl);
                    this.audioFlag = !this.audioFlag;
                    this.currentMusic = data.musicUrl;
                }
            }
            if (data.backgroundMusicUrl) {//预加载背景音频

                if (data.backgroundMusicUrl != this.currentBgMusic) {
                    let $audio = this.audioBgFlag ? $("#bgAudio2") : $("#bgAudio1");
                    $audio.attr("src", data.backgroundMusicUrl);
                    this.audioBgFlag = !this.audioBgFlag;
                    this.currentBgMusic = data.backgroundMusicUrl;
                }
            }
            if (data.videoUrl) {//预加载视频
                $("#video").attr("src", data.videoUrl);
                this.currentVideo = data.videoUrl;
            }
        }

    },
    progress: function () {
        // this.imgPreLoad(()=>{
        //     ++this.muCount;
        //     this.muPreLoadDone=true;
        // },(percent)=>{
        //     // console.log(percent)
        //     $(".progress-inner").css("width",percent+"%");
        // });
        let _this = this;
        let imgs = this.imgObj[this.muCount];
        if(!imgs) {
            _this.muPreLoadDone=true;
            return;
        }
        imgs = imgs.map((item) => {
            return item + "?x-oss-process=image/resize,w_375,limit_0";
        })
        var loader = new resLoader({
            resources: imgs,
            onStart: function (total) {
                $(".progress-inner").css("width", "0");
            },
            onProgress: function (current, total) {
                var percent = parseInt(current / total * 100);
                $(".progress-inner").css("width", percent + "%");
                $(".progress-text").text(percent + "%");
            },
            onComplete: function (total) {
                ++_this.muCount;
                localStorage.setItem("muCount", _this.muCount);
                _this.muPreLoadDone = true;
            }
        });

        loader.start();
    },
    imgPreLoad: function (complete, process) {//幕图片预加载 
        let imgs = this.imgObj[this.muCount];
        if (!imgs)
            return;
        let current = 0;
        imgs = imgs.map((item) => {
            return item + "?x-oss-process=image/resize,w_375,limit_0";
        })
        for (let i = 0; i < imgs.length; i++) {
            let image = new Image();
            image.onload = (function () {
                image.onload = null;
                current++;
                let percent = parseInt(current / imgs.length * 100);//进度
                process(percent);
                if (current == imgs.length) {
                    complete();
                }
            })();
            image.src = imgs[i];
        }
    },
    getCookie: function (name) {
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        return (arr = document.cookie.match(reg)) ? unescape(arr[2]) : null;
    },
    getUser: function () {
        let img = this.getCookie("wxImg");
        this.headImg = img;
        // $(".end-head-img").css("background","url("+img+"?x-oss-process=image/resize,w_184,limit_0) 0 0/100% no-repeat");
        $(".reply-head-img").css("background", "url(" + img + "?x-oss-process=image/resize,w_80,limit_0) 0 0/100% no-repeat");
    },
    start: function (img1, img2) {
        let _this = this;
        let imgs = [
            "http://rpgstaticoss.gjgj.com/App/images/img01.png",
            "http://rpgstaticoss.gjgj.com/App/images/img02.png",
            "http://rpgstaticoss.gjgj.com/App/images/img03.png",
            "http://rpgstaticoss.gjgj.com/App/images/img05.png",
            "http://rpgstaticoss.gjgj.com/App/images/img06.png",
            "http://rpgstaticoss.gjgj.com/App/images/img07.png"
        ];
        imgs.push(img1);
        imgs.push(img2);
        var loader = new resLoader({
            resources: imgs,
            onStart: function (total) {
                $(".progress-inner-start").css("width", "0%");
            },
            onProgress: function (current, total) {

                var percent = parseInt(current / total * 100);
                console.log(percent)
                $(".progress-inner-start").css("width", percent + "%");
                $(".progress-text-start span").text(percent + "%");
            },
            onComplete: function (total) {
                $(".play-icon").show();
                $(".play-start-wrap").fadeIn().siblings(".hideItem").hide();
                _this.progress();
                _this.muPreLoadDone=true;
            }
        });

        loader.start();
    },
    getData: function () {
        let _this = this;
        $(".play-start-load").show();
        // $(".play-start-wrap").show();
        // let url="http://rpg.gjgj.com/Sysconfig/getScenario";
        let url = "http://rpg.gjgj.com/Scenario/getScenario";
        $.ajax({
            type: "POST",
            url: url,
            data: "",
            dataType: "json",
            success: function (res) {
                $(".play-end").hide();
                let data = res.data;
                if (res.status == 1 && data) {
                    _this.data = data.node;
                    _this.role = data.role;
                    for (let key in _this.role) {
                        _this.role[key].score = 0;
                    }
                    //let step=location.search.substr(1);
                    _this.startId = data.scenarioInfo.startId;
                    //_this.step=step || data.scenarioInfo.startId;
                    let stepObj = localStorage.getItem("stepObj");
                    let roleObj = localStorage.getItem("role");
                    let endObj = localStorage.getItem("dayEndCount");
                    let saveGame = localStorage.getItem("saveGame");
                    let muCount = localStorage.getItem("muCount");
                    if (saveGame) {
                        _this.saveGameList = JSON.parse(saveGame);
                    }
                    if (muCount) {
                        _this.muCount = muCount;
                    }
                    if (stepObj) {
                        stepObj = JSON.parse(stepObj);
                        _this.step = stepObj.step;
                        _this.dialogList = stepObj.dialogList;
                        _this.dialogIndex = stepObj.dialogIndex;
                        if (roleObj) {
                            roleObj = JSON.parse(roleObj);
                            _this.role = roleObj;
                        }
                        if (endObj) {
                            _this.dayEndCount = JSON.parse(endObj);
                        }
                    } else {
                        _this.step = data.scenarioInfo.startId;
                    }
                    if ($("#startId").val() && $("#startId").val() != 0) {
                        _this.step = $("#startId").val();
                    }

                    // _this.step=1;//卷卷结局250；强强侠318;长歌342；拐子248；
                    _this.proloadData();

                    _this.imgObj = data.forestImgList;
                    // _this.progress();
                    if (data.scenarioInfo.img) {
                        $("#bg").css("background", "url(" + data.scenarioInfo.img + "?x-oss-process=image/resize,w_750,limit_0) 0 0/100% no-repeat").fadeIn();
                    }
                    _this.start(data.scenarioInfo.img + "?x-oss-process=image/resize,w_750,limit_0", data.forestImgList["1"][0] + "?x-oss-process=image/resize,w_750,limit_0");
                }
            }
        })
    }
}
window.onload = function () {
    wx.config({
        debug: false,
        appId: $("#appId").val(),
        timestamp: $("#timestamp").val(),
        nonceStr: $("#nonceStr").val(),
        signature: $("#signature").val(),
        jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline', 'onMenuShareQQ']
    });
    wx.ready(function () {
        share();
    });
    index.init();
};
const share = function () {
    let title = "在恋爱试用期我打出了" + index.saveGameList.length + "个结局，你行吗？"
    wx.onMenuShareAppMessage({
        title: title, // 分享标题
        desc: $("#shareDesc").val(), // 分享描述
        link: $("#shareUrl").val(), // 分享链接
        imgUrl: $("#shareImgUrl").val() + "?x-oss-process=image/resize,m_fixed,h_100,w_100&v=1513930793895", // 分享图标
        type: '', // 分享类型,music、video或link，不填默认为link
        dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
        success: function () {
            // 用户确认分享后执行的回调函数
        },
        cancel: function () {
            // 用户取消分享后执行的回调函数
        }
    });
    wx.onMenuShareTimeline({
        title: title, // 分享标题
        link: $("#shareUrl").val(), // 分享链接
        imgUrl: $("#shareImgUrl").val() + '?x-oss-process=image/resize,m_fixed,h_100,w_100&v=1513930793895', // 分享图标
        success: function () {
            // 用户确认分享后执行的回调函数
        },
        cancel: function () {
            // 用户取消分享后执行的回调函数
        }
    });
}
